package com.tunan.system.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.EasyUiPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tunan.common.PagePermissionUtil;
import com.tunan.common.Result;
import com.tunan.common.resultSub.ResultFailure;
import com.tunan.common.resultSub.ResultSuccess;
import com.tunan.persistence.model.TSysUser;
import com.tunan.system.service.IdentifierGenerator;
import com.tunan.system.service.SysUserService;

@Controller
@RequestMapping(value = "SystemUser")
public class SystemUserController extends BaseController {
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private IdentifierGenerator identifierGenerator;
	

	private Logger log = Logger.getLogger(SystemUserController.class);

	@RequestMapping(value = "addUser", method = RequestMethod.GET)
	public ModelAndView addUser(String id) {
		ModelAndView mav = new ModelAndView();
		if (StringUtils.isNotBlank(id)) {
			mav.addObject("vo", sysUserService.selectOne(id));
		}
		mav.setViewName("/system/addUser");
		return mav;
	}

	@RequestMapping(value = "sysUserList", method = RequestMethod.GET)
	public ModelAndView sysUserList(String menuId, HttpServletRequest request) {
		if(StringUtils.isNotBlank(menuId)){
			request.setAttribute("pagePermission", PagePermissionUtil.getPRight(menuId, request.getSession()));
		}
		return new ModelAndView("/system/sysUserList");
	}

	/**
	 * 查询用户
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "queryAll", method = RequestMethod.POST)
	@ResponseBody
	public EasyUiPage list(@ModelAttribute("pageParams") Page pageParams,
			TSysUser params) {
		PageHelper.startPage(pageParams.getPageNum(), pageParams.getPageSize());
		Page pageresult = (Page) sysUserService.findAll();
		return pageresult.getEasyUiPage();
	}
	
	@RequestMapping(value = "sysUserSelect", method = RequestMethod.GET)
	public ModelAndView sysUserSelect(String id, HttpServletRequest request) {
		return new ModelAndView("/system/sysUserSelect");
	}


	/**
	 * 新增和修改
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Result save(@ModelAttribute("user") TSysUser user) {
		ResultSuccess rs = new ResultSuccess();
		ResultFailure rf = new ResultFailure();
		try {
			if (user != null) {
				if (StringUtils.isNotBlank(user.getCzyid())) {
					sysUserService.update(user);
					rs.setMessage("修改成功");
				} else {
					user.setCzyid(identifierGenerator.generate());
					sysUserService.insertSelective(user);
					rs.setMessage("新增成功");
				}

			}
			return rs;

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		rf.setMessage("操作失败");
		return rf;
	}

	/**
	 * 删除
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Result delete(String id) {
		try {
			if (StringUtils.isNotBlank(id)) {
				sysUserService.delete(id);
			}
			return new ResultSuccess("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return new ResultFailure("删除失败");
	}

	/**
	 * 批量删除
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "batchDel", method = RequestMethod.POST)
	@ResponseBody
	public Result batchDel(String ids) {
		try {
			if (StringUtils.isNotBlank(ids)) {
				List<String> idList = new ArrayList<String>();
				if (ids.contains(",")) {
					idList = Arrays.asList(ids.split(","));
				} else {
					idList.add(ids);
				}
				sysUserService.batchDel(idList);
			}
			return new ResultSuccess("批量删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return new ResultFailure("批量删除失败");
	}

}
